package com.durian.controller;

import com.durian.domain.DTO.ObsBookAddDTO;
import com.durian.domain.DTO.ObsBookListDTO;
import com.durian.domain.entity.ObsBook;
import com.durian.service.IObsBookService;
import com.durian.utils.Result;
import jakarta.validation.constraints.Positive;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 书籍管理
 *
 * @author Durian
 * @since 2023-11-23
 */
@Validated
@RestController
@RequestMapping("book/books")
@RequiredArgsConstructor
public class ObsBookController {

    private final IObsBookService bookService;

    /**
     * 获取书籍
     */
    @PostMapping("list")
    public Result listBook(
            @RequestBody ObsBookListDTO bookListDTO
    ) {
        return Result.success(bookService.listBook(bookListDTO));
    }

    /**
     * 获取书籍(不分页)
     */
    @GetMapping("list")
    public Result listBook(
            @RequestParam
            String search
    ) {
        return Result.success(bookService.listBook(search));
    }

    /**
     * 新增书籍
     */
    @PostMapping("add")
    public Result addBook(
            @Validated
            @RequestBody ObsBookAddDTO bookAddDTO
    ) {
        bookService.addBook(bookAddDTO);
        return Result.success();
    }

    /**
     * 更新书籍
     */
    @PutMapping("update")
    public Result updateBook(
            @RequestBody ObsBook book
    ) {
        bookService.updateBook(book);
        return Result.success();
    }

    /**
     * 移除书籍
     */
    @PutMapping("remove")
    public Result removeBook(
            @RequestBody
            List<@Positive(message = "书籍id必须非零为正数") Long> ids
    ) {
        bookService.removeBook(ids);
        return Result.success();
    }

}
